package manage.commons.cookie;

import static manage.commons.cookie.UserStateConstants.COOKIE_USER_LOGINTIME;
import static manage.commons.cookie.UserStateConstants.COOKIE_USER_PASSWORD;
import static manage.commons.cookie.UserStateConstants.COOKIE_USER_REMEMBERME;
import static manage.commons.cookie.UserStateConstants.COOKIE_USER_SUPERMANAGER;
import static manage.commons.cookie.UserStateConstants.COOKIE_USER_USERID;

import org.apache.commons.lang3.StringUtils;

public class UserStateSessionManager {

	private SessionManager sessionManager;
	private UserState userState;

	public UserStateSessionManager(UserState userState) {
		this(userState.getUserId(), userState.getPassword(), userState
				.getLoginTime(), userState.getRemember(), userState
				.getSuperManager());
	}

	public UserStateSessionManager(Integer userId, String password,
			long loginTime, int rememberMe, int superManager) {
		sessionManager = new DefaultSessionManager();
		sessionManager.setAttribute(COOKIE_USER_USERID, userId);
		sessionManager.setAttribute(COOKIE_USER_PASSWORD, password);
		sessionManager.setAttribute(COOKIE_USER_LOGINTIME, loginTime);
		sessionManager.setAttribute(COOKIE_USER_REMEMBERME, rememberMe);
		sessionManager.setAttribute(COOKIE_USER_SUPERMANAGER, superManager);
		UserState userState = new UserState(userId, password, loginTime,
				rememberMe, superManager);
		setUserState(userState);
	}

	public UserStateSessionManager(String cookieValue) {
		sessionManager = new DefaultSessionManager(cookieValue);
		UserState userState = getUserFormCookie(cookieValue);
		setUserState(userState);
	}

	public String toCookieString() {
		return sessionManager.getSessionString();
	}

	public UserState getUserFormCookie(String cookieValue) {
		if (StringUtils.isNotEmpty(cookieValue)) {
			String userId = sessionManager.getAttribute(COOKIE_USER_USERID);
			String password = sessionManager.getAttribute(COOKIE_USER_PASSWORD);
			String loginTime = sessionManager
					.getAttribute(COOKIE_USER_LOGINTIME);
			String rememberMe = sessionManager
					.getAttribute(COOKIE_USER_REMEMBERME);
			String superManager = sessionManager
					.getAttribute(COOKIE_USER_SUPERMANAGER);
			if (StringUtils.isNotEmpty(userId)
					&& StringUtils.isNotEmpty(password)
					&& StringUtils.isNotEmpty(loginTime)
					&& StringUtils.isNotEmpty(rememberMe)
					&& StringUtils.isNotEmpty(superManager)) {
				UserState userState = new UserState();
				userState.setUserId(Integer.parseInt(userId));
				userState.setPassword(password);
				userState.setLoginTime(Long.valueOf(loginTime));
				userState.setRemember(Integer.parseInt(rememberMe));
				userState.setSuperManager(Integer.parseInt(superManager));
				return userState;
			}
		}
		return null;
	}

	public boolean online(UserState dbUser) {
		if (userState.isRememberMe()) {
			return checkUserOnline(userState, dbUser);
		} else {
			if (!userState.isLoginTimeOut()) {
				return checkUserOnline(userState, dbUser);
			}
		}
		return false;
	}

	public boolean isSuperManager() {
		return getUserState().isSuperManager();
	}

	private boolean checkUserOnline(UserState userState, UserState dbUser) {
		if (StringUtils.equals(String.valueOf(userState.getUserId()),
				String.valueOf(dbUser.getUserId()))
				&& StringUtils.equals(userState.getPassword(),
						dbUser.getPassword())) {
			return true;
		}
		return false;
	}

	public void setUserState(UserState userState) {
		this.userState = userState;
	}

	public UserState getUserState() {
		return userState;
	}

}
